﻿using System;
using System.Collections.Generic;

namespace CSharp进阶_LinkedList
{
	class Program
	{
		static void Main(string[] args)
		{
			#region 声明
			LinkedList<int> list = new LinkedList<int>();
			#endregion
			#region 增
			// 1.尾插
			list.AddLast(1);
			list.AddLast(2);
			list.AddLast(3);
			// 2.头插
			list.AddFirst(4);
			list.AddFirst(5);
			list.AddFirst(6);
			Print(list);
			// 3.在某个元素之后插入
			list.AddAfter(list.Find(6), 100);
			// 4.在某个元素之前插入
			list.AddBefore(list.Find(100), 200);
			#endregion
			#region 删
			list.Remove(1);
			list.Remove(30);
			Print(list);
			#endregion
			#region 查
			if (list.Contains(6))
			{
				Console.WriteLine("包含6");
			}
			else
			{
				Console.WriteLine("不包含6");
			}
			LinkedListNode<int> node = list.Find(4);
			if (node != null)
			{
				Console.WriteLine("找到4");
			}
			else
			{
				Console.WriteLine("没有找到4");
			}
			node = list.FindLast(4);
			if (node != null)
			{
				Console.WriteLine("找到4");
			}
			else
			{
				Console.WriteLine("没有找到4");
			}
			#endregion
			#region 改
			node = list.Find(4);
			if (node != null)
			{
				node.Value *= 10;
			}
			#endregion
			#region 遍历
			Console.WriteLine("\n 正向foreach");
			foreach (int item in list)
			{
				Console.Write(item + " ");
			}
			Console.WriteLine("\n");
			Console.WriteLine("\n 正向 node");
			LinkedListNode<int> cur = list.First;
			while (cur != null)
			{
				Console.Write(cur.Value + " ");
				cur = cur.Next;
			}
			Console.WriteLine("\n");

			Console.WriteLine("\n 反向 node");
			cur = list.Last;
			while (cur != null)
			{
				Console.Write(cur.Value + " ");
				cur = cur.Previous;
			}
			Console.WriteLine("\n");
			#endregion
		}
		static void Print(LinkedList<int> list)
		{
			Console.WriteLine();
			Console.WriteLine("---------------------");
			LinkedListNode<int> node = list.First;
			while (node != null)
			{
				Console.Write(node.Value + "<->");
				node = node.Next;
			}
			Console.WriteLine("\n---------------------");
			Console.WriteLine();
		}
	}
}
